﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using BasicFrameworkDemo.Utility;
using Castle.Facilities.NHibernate;
using Castle.Transactions;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;

namespace BasicFrameworkDemo.IDAL
{
    public class RepositoryBase : INHibernateInstaller 
    {
        protected virtual string ConnectionStr { get; set; }

        private readonly Maybe<IInterceptor> interceptor;

        public RepositoryBase()
		{
			interceptor = Maybe.None<IInterceptor>();
		}

        public RepositoryBase(IInterceptor interceptor)
		{
			this.interceptor = Maybe.Some(interceptor);
		}

        public FluentConfiguration BuildFluent()
        {
            var nConfig =
                Fluently.Configure()
                    .Database(
                        MsSqlConfiguration.MsSql2008.ConnectionString(
                            str => str.FromConnectionStringWithKey(ConnectionStr))
                            .AdoNetBatchSize(100)
                    )
                    .Mappings(
                        x => x.FluentMappings.AddFromAssembly(Assembly.Load(SystemConfig.FluentNHibernateAssembly)))
                    .CurrentSessionContext("web");

            return nConfig;
        }

        public void Registered(ISessionFactory factory)
        {
            
        }

        public virtual bool IsDefault
        {
            get { return false; }
        }

        public virtual string SessionFactoryKey
        {
            get { return string.Empty; }
        }

        public Maybe<IInterceptor> Interceptor
        {
            get { return interceptor; }
        }
    }
}
